<div class="content">
	<div class="list_button">
        <table cellpadding="0" cellspacing="0" border="0" width="100%" height="100%">
        <tr>
            <td>
                <?php
				
					echo CHtml::link(
						"<div class='button'>" .
							"<div style='float:left; width:20px;'>" .
								CHtml::image(Yii::app()->baseUrl."/assets/icon/icon-16-plus.png") .
							"</div>" .
							"<div style='float:left; width:65px;'>".
								"Thêm mới" .
							"</div>" .
						"</div>"
						,
                       array($this->controller_id."/add",
					   	"parent_id"=>$parent_id, "page"=> $page)
                    );
					
					echo "<div class='button' id='delete_multi'>" .
							"<div style='float:left; width:20px;'>" .
								CHtml::image(Yii::app()->baseUrl."/assets/icon/icon-16-remove.png") .
							"</div>" .
							"<div style='float:left; width:30px;'>".
								"Xóa" .
							"</div>" .
						"</div>";
						
					echo "<div style='color:#f00; font-size:12px; padding-top:7px;'>";
						echo "<strong>* Chú ý:</strong> Chỉ xóa các chuyên mục rỗng.";
					echo "</div>";
                ?>
                
                <script language="javascript">
                $(document).ready(function(){
                    $('#delete_multi').click(function(){
                        var form = document.getElementById("form_table_all");
                        form.submit();
                    });	
                })
              </script>
              
            </td>
            
            <td align="right">
                <?php
                    $list_acategory = array();
                    $list_acategory[0] = "-- Chuyên mục --";
					
                    $parent = acategory::model()->parent()->findAll();
					
                    if (!is_null($parent))
                    {
                        foreach($parent as $p)
                        {
                            $list_acategory[$p["id"]] = $p["name"];
                        }
                    }
                    
                    echo CHtml::dropDownList("parent_id",$parent_id, $list_acategory,
                        array("id"=>"parent_id", "style"=>"margin:0"));
            
                ?>
                  <script language="javascript">
                    $(document).ready(function(){
                        $('#parent_id').change(function(){
                            <?php
                                $url = Yii::app()->createUrl($this->getUniqueId()."/view");
                            ?>
                            var url = '<?php echo $url; ?>' + 
								"&parent_id=" + this.value + "&page=" + <?php echo $page; ?>;
                            window.location = url;
                        });	
                    })
                  </script>
             </td>
          </tr>
        </table>
	</div>
   
        <table class="view">
            <tr height='25'>
            	<th width="50px"> # </th>
                 <th width="10">
					<?php 
						echo CHtml::checkBox("checkbox_all", false, array("id"=>"checkbox_all"));
					?>
                </th>
            	<th colspan="2" width="10">Action</th>
                <th>Chuyên mục</th>
                <th width="80">Bật / Tắt</th>
                <th width="100">
					<?php 
                        echo CHtml::link(
                            "Thứ tự",
                            array($this->controller_id."/set_order", "parent_id"=>$parent_id, "page"=>$page),
                            array("title"=>"Tạo thứ tự mặc định")
                        );
                        echo " ";
                        echo CHtml::image(Yii::app()->baseUrl."/assets/icon/icon-16-saveorder.png", "", 
                            array("style"=>"cursor:pointer", "id"=>"id_save_order"));
    
                    ?>
                    <script language="javascript">
						$(document).ready(function(){
							$('#id_save_order').click(function() {
								var url = '<?php 
									$url = Yii::app()->createUrl(
										$this->route."/save_order",
										array("parent_id"=>$parent_id, "page"=>$page)
									);
									echo $url;
								?>';
								var form = document.getElementById("form_table_all");
								form.action = url;
								//alert(form.action);
								form.submit();
							});
						});
					</script>
                </th>
                <th width="120">Alias</th>
                <th width="70"># Bài viết</th>
                <th width="70"># Bật</th>
                <th width="70"># Rác</th>
                
                <th width="100">Parent</th>
                <th width="50">ID</th>
            </tr>
            <?php
                if (! is_null($rows))
                {
					echo CHtml::beginForm(
						array(
							$this->controller_id."/delete_multi",
							"parent_id"=> $parent_id,
							"page" => $page
						),
						"post",
						array("id"=>"form_table_all")
					);
					
					$i = 1;
                    foreach($rows as $row)
                    {
						show_acategory($row, $parent_id, $this->route, $this->controller_id, $page, $i );
						$i++;
                    }
					
					echo CHtml::endForm();
                }
				
				function show_acategory($row, $parent_id, $route, $controller_id, $page, $i)
				{
					
					 echo "<tr>";
						echo "<td align='center'>";
							echo $i;
						echo "</td>";
						
						echo "<td>";
							echo CHtml::checkBox("cib[".$row["id"]."]", false, array("id"=>"cb".$i));
						echo "</td>";
						
						echo "<td width='26px' align='center'>";
							$url = Yii::app()->createUrl(
								"content/" . $controller_id. "/delete",
								array(
									"acategory_id" => $row["id"],
									"parent_id" => $parent_id,
									"page" => $page
								)
							);
							$count_sub_category = count($row->sub_category);
							$count_article = count($row->articles);
							if ($count_sub_category == 0 && $count_article == 0)
							{
								echo CHtml::image(
									Yii::app()->baseUrl.'/assets/icon/icon-16-trash.png',
									"",
									array(
										"style"=>"cursor:pointer",
										"title"=>"Delete",  
										"onClick"=>"javascript:actionDelete('".$url."');"
									)
								);
							}
							else
							{
								$message = "Bạn không thể xóa chuyên mục này vi chuyên mục không rỗng.";
								echo CHtml::image(
									Yii::app()->baseUrl.'/assets/icon/icon-16-trash.png',
									"",
									array(
										"style"=>"cursor:pointer",
										"title"=>"Delete",  
										"onClick"=>"javascript:actionAlert('".$message."');"
									)
								);
							}
						echo "</td>";
						
						echo "<td width='26px' align='center'>";
							echo CHtml::link(
								CHtml::image(Yii::app()->baseUrl.'/assets/icon/icon-16-edit.png'),
								array(
									$controller_id."/edit",
									"acategory_id"=>$row["id"],
									"parent_id" => $parent_id,
									"page" => $page
								),
								array('title'=>'Edit')
							);
						echo "</td>";
						
						echo "<td>";
							if ($row["parent_id"] > 0)
								echo CHtml::image(Yii::app()->baseUrl.'/assets/icon/icon-16-triangle.png');
							echo CHtml::link(	
								$row["name"],
								array(
									$controller_id."/edit",
									"acategory_id"=>$row["id"],
									"parent_id" => $parent_id,
									"page" => $page
								),
								array('title'=>'Edit')
							);
						echo "</td>";
						
						echo "<td align='center'>";
							$icons =  array(
								0 => "icon-16-remove.png",
								1 => "icon-16-tick.png",
							);
							echo CHtml::link(
								CHtml::image(Yii::app()->baseUrl.'/assets/icon/'.$icons[$row["enable"]]),
								array($controller_id."/enable", 
									"acategory_id"=>$row["id"],
									"parent_id" => $parent_id,
									"page" => $page
								)
							);
						echo "</td>";
						
						echo "<td align='right'>";
							echo CHtml::textField(
								"order[".$row->id."]",
								$row->order,
								array("style"=>"width:40px; text-align:right; 
									padding:3px; color:#f00; font-size:11px;")
							);
							echo " ";
							
							
							echo CHtml::link(
								CHtml::image(Yii::app()->baseUrl."/assets/icon/icon-16-up.png"),
								array(
									$controller_id."/up",  
									"adminmenu_id" => $row->id, 
									"parent_id" => $parent_id)
							);
							
							echo CHtml::link(
								CHtml::image(Yii::app()->baseUrl."/assets/icon/icon-16-down.png"),
								array(
									$controller_id."/down", 
									"adminmenu_id" => $row->id, 
									"parent_id" => $parent_id)
							);
							
						echo "</td>";
						
						echo "<td>";
							echo $row->alias;
						echo "</td>";
						
						echo "<td align='center'>";
							$articles = $row->articles;
							echo count($articles);
						echo "</td>";
						
						echo "<td align='center'>";
							$articles_enable = $row->articles(array("condition"=>"enable = 1"));
							echo count($articles_enable);
						echo "</td>";
						
						echo "<td align='center'>";
							$articles_trash = $row->articles(array("condition"=>"trash = 1"));
							echo count($articles_trash);
						echo "</td>";
						
						echo "<td>";
							if ($row["parent_id"] == 0)
								echo "<span style='color:red;'>"."NULL" ."</span>";
							else{
								$parent = acategory::model()->findByPk($row["parent_id"]);
								echo $parent->name;
							}
						echo "</td>";
			
						echo "<td align='center'>";
							echo $row["id"];
						echo "</td>";
					echo "</tr>";  	
				}
            ?>
        </table>
    </div>
</div><!-- end div right -->

<?php 
	// display pagination
	if ($pages->pageCount > 1)
	{
		echo "<div class='pagination'>";
			$this->widget('CLinkPager', array(
				'header' => "",
				'cssFile' => Yii::app()->baseUrl."/assets/css/pager.css",
				"prevPageLabel" => "«",
				"nextPageLabel" => "»",
				'pages' => $pages,
			));
			
			$this->widget('CListPager', array(
				//"promptText" => "Chuyển nhanh",
				"header" => "Trang ",
				'pages'=>$pages
			));
		echo "</div>";
	}
?>

<script language="javascript">
function actionDelete(url)
{	
	if (confirm("Bạn muốn xóa mẫu tin này ?") == true)
		window.location = url;
}

function actionAlert(message)
{
	alert(message);
}

</script>